#!/bin/bash

pro_name=$1
cmd=$2
k=1
allpid=0
pid=0

allpid=`ps -ef | grep $pro_name | grep -v grep | awk '{print $2}' `
pid=`echo $allpid | cut -d ' ' -f 1`
echo $allpid
echo $pid

num=`ps -ef | grep $pro_name | grep -v grep | grep -v defunct  | wc -l`
echo $num

if [ $pro_name = "cloudsf" ] && [ $cmd = "abc" ];then
	kill $allpid
exit 0
fi

if [ $pro_name = "cloudsm" ] && [ $cmd = "killcloudsm" ];then
	kill $pid
exit 0
fi

if [ $num -le 2 ];then
	exec $cmd
else
	((num=$num-$k))
    if [ $num -le 2 ] && [ $pro_name = "cloudsf" ];then
	    kill $pid
	    exec $cmd
    fi
fi
